﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.JavaScript.JQuery;
using blackhouse.BlackApart.Business.Opinions;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Company;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.BlackApart.Web;
using System.Globalization;
using blackhouse.WebNemo.Web;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Web.Navigation;

namespace blackhouse.Aparts.Web.Modules {
    public partial class FrontBaner : System.Web.UI.UserControl {

        #region Fields

        private IOpinion[] selectedOpinions;
        private ICompany company;
        private float averageRating;

        #endregion

        #region Event Handlers

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.rptOpinions.ItemDataBound += new RepeaterItemEventHandler(rptOpinions_ItemDataBound);

            this.SetInstance();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.PopulateControls();
            this.PopulateEvent();
        }

        private void PopulateEvent() {
            GroupRecord record = null;
            try {
                GroupRecords records = new GroupRecords("wydarzenia_" + CultureInfo.CurrentCulture.Name.Split('-').First());
                var r2 = records.Where(r => r.GetBoolean("na_glowna") && r.IsNotNull("data_od") && r.GetDateTime("data_od") >= DateTime.Now.Date);

                if (!blackhouse.WebNemo.Business.Factory.Instance().GetCurrentUser().HasSufficientPermissions(false))
                    r2 = r2.Where(ev => ev.GetBoolean("publiczna"));

                if (r2.Count() == 0)
                    record = records.OrderByDescending(r => r.CreatedTime).FirstOrDefault();
                else
                    record = r2.OrderBy(r => r.GetDateTime("data_od")).FirstOrDefault();
            }
            catch (Exception) {
            }

            if (record != null) {
                this.pnlEvent.BackImageUrl = record.GetImageUrl("zdjecie", new System.Drawing.Size(410, 1000));
                this.lnkEvent.Text = record.GetString("tytul");
                this.lnkEvent.NavigateUrl = UrlUtils.LocalizeUrl("/" + record.GetString("url"));
            }
            else {
                this.pnlEvent.Visible = false;
                this.pnlAcom.Visible = true;
            }
        }

        void rptOpinions_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:

                    IOpinion opinion = e.Item.DataItem as IOpinion;
                    OpinionItem item = e.Item.FindControl("item") as OpinionItem;

                    if (opinion != null && item != null) {
                        item.Opinion = opinion;
                        item.DataBind();
                    }

                    break;
                default:
                    break;
            }
        }

        #endregion

        #region Methods

        private void SetInstance() {
            Factory factory = Factory.Instance();
            this.selectedOpinions = factory.GetOpinion().GetOpinions().OrderByDescending(o => o.PublishedDate).Take(2).ToArray();
            this.company = factory.GetCompany();
            this.averageRating = factory.GetStatisticsOpinion().GetAverageRating();
        }

        private void PopulateControls() {
            this.rptOpinions.DataSource = this.selectedOpinions;
            this.rptOpinions.DataBind();

            this.ltlClients.Text = this.company.GetCounts().Clients.ToString();
            this.ltlPhotos.Text = this.company.GetCounts().Photos.ToString();
            this.ltlOpinions.Text = this.company.GetCounts().Opinions.ToString();
            this.ltlOpinionValue.Text = this.averageRating.ToString("F2");
        }

        protected void bntCheckReservation_Click(object sender, EventArgs e) {
            string reservationNumber = Request[this.txtReservationNumber.UniqueID];
            if (!string.IsNullOrEmpty(reservationNumber)) {
                IWebReservation reservation = Factory.Instance().GetWebReservation(reservationNumber);
                if (reservation.ReservationId > 0) {
                    string url = NavigationProvider.Instance().GetUrl(ReservationPage.GetStatusPage(reservation));
                    if (!string.IsNullOrEmpty(url)) {
                        Response.Redirect(url, true);
                        return;
                    }
                }
            }
            // gdzieś był błąd to pokaż info o niepoprawnym numerze
            this.pnlCheckReservationError.Visible = true;
        }

        #endregion

    }
}